package com.huike.web.controller.contract;

import com.huike.clues.service.TbClueService;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.contract.domain.TbContract;
import com.huike.contract.domain.dto.TbContractDTO;
import com.huike.contract.mapper.TbContractMapper;
import com.huike.contract.service.TbContractService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 合同Controller
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/contract")
public class TbContractController extends BaseController {

    private final TbContractService tbContractService;

    private final TbClueService tbClueService;
    private final TbContractMapper tbContractMapper;

    /**
     * 合同列表
     * @param tbContractDTO
     * @return
     */
    @ApiOperation("合同列表查询接口")
    @GetMapping("/list")
    public TableDataInfo list(TbContractDTO tbContractDTO){
        startPage();
        List<TbContractDTO> list = tbContractService.selectTbContract(tbContractDTO);
        return getDataTable(list);
    }

    /**
     * 新增合同
     * @param tbContract
     * @return
     */
    @ApiOperation("新增合同接口")
    @PostMapping
    public AjaxResult add(@RequestBody TbContract tbContract){
        log.info("新增合同信息:{}",tbContract);
        //if(!tbClueService.checkCluePhoneExis(tbContract.getPhone())) return error("手机号已存在");
        return AjaxResult.success(tbContractService.add(tbContract));
    }

    /**
     * 合同详细信息接口1
     * @param id
     * @return
     */
    @ApiOperation("合同详情信息接口1")
    @GetMapping("detail/{id}")
    public AjaxResult detail(@PathVariable("id") Long id){
        log.info("合同的id是：{}",id);
        TbContract tbContract = tbContractMapper.selectById(id);
        return AjaxResult.success(tbContract);
    }

    /**
     * 合同详细信息接口2
     * @param id
     * @return
     */
    @ApiOperation("合同详细信息接口2")
    @GetMapping("{id}")
    public AjaxResult selectById(@PathVariable("id") Long id){
        log.info("合同详情信息的id是：{}",id);
        TbContract tbContract = tbContractMapper.selectById(id);
        return AjaxResult.success(tbContract);
    }

    /**
     * 商机转合同
     * @param id
     * @param tbContract
     * @return
     */
    @ApiOperation("商机转合同接口")
    @PutMapping("changeContract/{id}")
    public AjaxResult changeContract(@PathVariable("id") Long id,@RequestBody TbContract tbContract){

        return toAjax(tbContractService.changeContract(id,tbContract));
    }


}
